package com.firebird.app.kotlin.stock.trend.view

import android.content.Context
import android.util.AttributeSet
import android.util.DisplayMetrics
import android.view.View
import android.view.animation.Animation
import android.view.animation.TranslateAnimation
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.viewpager.widget.ViewPager
import com.firebird.app.kotlin.stock.trend.R


/**
 * 自定义带动画效果的，viewpager Tab指示器控件
 * @author ljk
 * @date   2019年10月28日
 */
class AnimationTabWidget : RelativeLayout, View.OnClickListener {
    //private Context context;
    private var viewPager: ViewPager? = null
    private var img_cursor: ImageView? = null
    private var tv_tag1: TextView? = null
    private var tv_tag2: TextView? = null
    private var tv_tag3: TextView? = null
    private var tv_tag4: TextView? = null
    private var tv_tag5: TextView? = null
    private val whiteColor = resources.getColor(R.color.menu_gray)
    private val redColor = resources.getColor(R.color.menu_red)
    private var currentTab = 0
    private var imgWidth = 0

    constructor(context: Context?) : super(context, null) {}
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        //this.context = context;
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.animationTabWidget)
        currentTab = typedArray.getInt(R.styleable.animationTabWidget_current_index, 0)
        typedArray.recycle()
        View.inflate(context, R.layout.layout_animation_tab_widget, this)
        img_cursor = findViewById<View>(R.id.tag_img_cursor) as ImageView
        tv_tag1 = findViewById<View>(R.id.tv_tag1) as TextView
        tv_tag2 = findViewById<View>(R.id.tv_tag2) as TextView
        tv_tag3 = findViewById<View>(R.id.tv_tag3) as TextView
        tv_tag4 = findViewById<View>(R.id.tv_tag4) as TextView
        tv_tag5 = findViewById<View>(R.id.tv_tag5) as TextView
        tv_tag1!!.setOnClickListener(this)
        tv_tag2!!.setOnClickListener(this)
        tv_tag3!!.setOnClickListener(this)
        tv_tag4!!.setOnClickListener(this)
        tv_tag5!!.setOnClickListener(this)
        initDefaultIndex()
    }

    /**
     * 初始化 默认 指示位置
     */
    private fun initDefaultIndex() { //获取屏幕分辨率 宽度
        var dm= DisplayMetrics()
        dm = resources.displayMetrics
        val screenW = dm.widthPixels
        imgWidth = screenW / 5
        val para = img_cursor!!.layoutParams as LayoutParams
        para.width = imgWidth
        img_cursor!!.layoutParams = para
        setCurrentTab(currentTab)
    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.tv_tag1 -> currentTab = 0
            R.id.tv_tag2 -> currentTab = 1
            R.id.tv_tag3 -> currentTab = 2
            R.id.tv_tag4 -> currentTab = 3
            R.id.tv_tag5 -> currentTab = 4
        }
        if (viewPager != null) {
            viewPager!!.currentItem = currentTab
        }
    }

    fun setViewPager(viewPager: ViewPager?) {
        this.viewPager = viewPager
    }

    /**
     * 设置当前指示器 位置
     * @param position
     */
    fun setCurrentTab(position: Int) {
        tv_tag1!!.textSize = 12f
        tv_tag2!!.textSize = 12f
        tv_tag3!!.textSize = 12f
        tv_tag4!!.textSize = 12f
        tv_tag5!!.textSize = 12f
        tv_tag1!!.setTextColor(whiteColor)
        tv_tag2!!.setTextColor(whiteColor)
        tv_tag3!!.setTextColor(whiteColor)
        tv_tag4!!.setTextColor(whiteColor)
        tv_tag5!!.setTextColor(whiteColor)
        when (position) {
            0 -> {
                tv_tag1!!.setTextColor(redColor)
                tv_tag1!!.textSize = 16f
            }
            1 -> {
                tv_tag2!!.setTextColor(redColor)
                tv_tag2!!.textSize = 16f
            }
            2 -> {
                tv_tag3!!.setTextColor(redColor)
                tv_tag3!!.textSize = 16f
            }
            3 -> {
                tv_tag4!!.setTextColor(redColor)
                tv_tag4!!.textSize = 16f
            }
            4 -> {
                tv_tag5!!.setTextColor(redColor)
                tv_tag5!!.textSize = 16f
            }
        }
        var animation: Animation? = null
        animation = TranslateAnimation((imgWidth * currentTab).toFloat(), (imgWidth * position).toFloat(), 0f, 0f)
        animation.setFillAfter(true) //True:图片停在动画结束位置
        animation.setDuration(300) //设置动画完成时间
        img_cursor!!.startAnimation(animation)
        currentTab = position
    }
}